﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevComponents.DotNetBar;

namespace RM2012Server.Model
{
    public class Log
    {
        public String MakePath(String fileName)
        {
            String path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));
            path = path + "Log/" + fileName;
            return path;
        }

        public bool IsExistFile(String fileName)
        {
            bool check = false;
            try
            {
                String _filePath = MakePath(fileName);
                FileInfo f = new FileInfo(_filePath);
                if (f.Exists)
                {
                    check = true;
                }
            }
            catch
            {
                check = false;
            }
            return check;
        }

        public bool CreateLogAuto()
        {
            bool check = false;
            try
            {
                DateTime dateNow = DateTime.Now;
                String _year = dateNow.Year.ToString();
                String _month = dateNow.Month.ToString();
                if (dateNow.Month < 10)
                {
                    _month = "0" + dateNow.Month.ToString();
                }
                String _day = dateNow.Day.ToString();
                if (dateNow.Day < 10)
                {
                    _day = "0" + dateNow.Day.ToString();
                }
                String _end = _year + _month + _day;
                String _fileName = "Log_" + _end + ".txt";
                String _path = MakePath(_fileName);
                FileInfo f = new FileInfo(_path);
                if (!f.Exists)
                {
                    File.Create(_path);
                    check = true;
                }
            }
            catch
            {
                check = false;
            }
            return check;
        }

        public bool CreateLog(String fileName)
        {
            bool check = false;
            try
            {
                String _filePath = MakePath(fileName);
                FileInfo f = new FileInfo(_filePath);
                if (!f.Exists)
                {
                    File.Create(_filePath);
                    check = true;
                }
            }
            catch
            {
                check = false;
            }
            return check;
        }

        public String ViewLog(String _fileName)
        {
            try
            {
                String _content = "";
                String _filePath = MakePath(_fileName);
                StreamReader sReader = new StreamReader(_filePath, Encoding.Unicode);
                char[] strChar = new char[100000];
                sReader.Read(strChar, 0, strChar.Length);
                _content = new String(strChar, 0, strChar.Length);
                sReader.Close();
                return _content;
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }

        public bool WriteLog(String _fileName, String _message)
        {
            bool check = false;
            try
            {
                String _filePath = MakePath(_fileName);
                FileInfo f = new FileInfo(_filePath);
                if (!f.Exists)
                {
                    File.Create(_filePath);
                }
                StreamWriter sWriter = new StreamWriter(_filePath, true, Encoding.Unicode);
                sWriter.WriteLine(_message);
                sWriter.Flush();
                sWriter.Close();
                check = true;
            }
            catch
            {
                check = false;
            }
            return check;
        }
    }
}
